package com.gxc.controller;

import com.gxc.utils.RestControllerResult;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传Controller.
 *
 * @author 宫新程
 * @since 2018/12/18 1:07 PM
 */
@Slf4j
@RestController
public class UploadController {

  /** 单个文件上传. */
  @RequestMapping(value = "/upload", method = RequestMethod.POST)
  public RestControllerResult<Boolean> upload(MultipartFile file) {
    RestControllerResult<Boolean> result = new RestControllerResult<>();
    try {

      readZipFile(file.getBytes());

      result.setSuccess(true);
    } catch (Exception e) {
      log.error("文件上传失败！：{}", e);
      result.setSuccess(false);
    }
    return result;
  }

  public void readZipFile(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ZipInputStream zipInputStream = new ZipInputStream(bis);
    try {
      ZipEntry zipEntry;
      byte[] byteEntry = null;
      while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        if (zipEntry.isDirectory()) {
          // do nothing
        } else {
          String name = zipEntry.getName();
          long size = zipEntry.getSize();
          // unknown size
          // ZipEntry的size可能为-1，表示未知
          // 通过上面的几种方式下载，就会产生这种情况
          if (size == -1) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while (true) {
              int byteint = zipInputStream.read();
              if (byteint == -1) break;
              baos.write(byteint);
            }
            baos.close();
            byteEntry = baos.toByteArray();
            System.out.println(
                String.format("Name:%s,Content:%s", name, new String(baos.toByteArray())));
          } else { // ZipEntry的size正常
            byteEntry = new byte[(int) zipEntry.getSize()];
            zipInputStream.read(byteEntry, 0, (int) zipEntry.getSize());
            System.out.println(String.format("Name:%s,Content:%s", name, new String(byteEntry)));
          }
          InputStreamReader read =
              new InputStreamReader(new ByteArrayInputStream(byteEntry), "gb2312");
          BufferedReader bufferedReader = new BufferedReader(read);
          String line = "";
          line = bufferedReader.readLine();
          while (line != null) {
            line = bufferedReader.readLine(); // 一次读入一行数据
            if (line == null) { // 最后一行异常
              break;
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (bis != null) {
        try {
          bis.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      if (zipInputStream != null) {
        try {
          zipInputStream.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }
  }
}
